# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： recognition_result_process
Description :
Author : 'li'
date： 2020/10/29
-------------------------------------------------
Change Activity:
2020/10/29:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.column_sort_util import ColumnSortUtil
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.generate_sort_result import \
    SortedResultGenerator
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.row_sort_util import RowSortUtil
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.text_area_description import \
    TextAreaDescription


class RecognitionResultProcessor:
    def __init__(self, max_textarea_distance=100):
        self.original_results = []
        self.textarea_descriptions = []
        self.container_direction_type = 'mix'  # vertical、horizontal、mix
        self.max_textarea_distance = max_textarea_distance
        self.sorted_lines = []
        self.sorted_textarea = None
        self.result_generator = None

    def add_recognition_result(self, textarea=None, bbox=None, recognition_content=None):
        """
        :param textarea:
        :param bbox:
        :param recognition_content:
        :return:
        """
        item = {'text_area': textarea, 'bbox': bbox, 'recognition_content': recognition_content}
        self.original_results.append(item)

    def _convert_to_textarea_description(self):
        for result in self.original_results:
            description = TextAreaDescription(content_vector=result['text_area'],
                                              box_coordinate=result['bbox'],
                                              recognition_result=result['recognition_content'])
            self.textarea_descriptions.append(description)

    def get_final_result(self):
        self._convert_to_textarea_description()
        self._infer_container_direction_type()
        self._infer_result()
        return self.result_generator

    def _infer_result(self):
        """
        :return:
        """
        if self.container_direction_type == 'horizontal':
            self.sorted_textarea = self._infer_horizontal_direction_image()
            res_generator = SortedResultGenerator(self.sorted_textarea)
            res_generator.get_direction_result()
            self.result_generator = res_generator
        elif self.container_direction_type == 'vertical':
            self.sorted_textarea = self._infer_vertical_direction_image()
            res_generator = SortedResultGenerator(self.sorted_textarea)
            res_generator.get_direction_result()
            self.result_generator = res_generator
        else:
            self.sorted_textarea = self._infer_horizontal_direction_image()
            res_generator = SortedResultGenerator(self.sorted_textarea)
            res_generator.get_direction_result()
            self.result_generator = res_generator
            pass
            if not self.result_generator.is_valid_container_number:
                self.sorted_textarea = self._infer_vertical_direction_image()
                res_generator = SortedResultGenerator(self.sorted_textarea)
                res_generator.get_direction_result()
                self.result_generator = res_generator
            return self.result_generator

    def _infer_horizontal_direction_image(self):
        """
        :return:
        """
        row_sort_util = RowSortUtil(self.textarea_descriptions, max_interval=self.max_textarea_distance)
        sorted_row = row_sort_util.sort_row()
        return sorted_row

    def _infer_vertical_direction_image(self):
        """
        :return:
        """
        row_sort_util = ColumnSortUtil(self.textarea_descriptions, max_interval=self.max_textarea_distance)
        return row_sort_util.sort_column()

    def _infer_container_direction_type(self):
        horizontal_size = 0
        vertical_size = 0
        check_code_size = 0
        for description in self.textarea_descriptions:
            textarea_type = description.textarea_type
            if textarea_type is None:
                continue
            if textarea_type == 'vertical':
                vertical_size = vertical_size + 1
                continue
            if textarea_type == 'horizontal':
                horizontal_size = horizontal_size + 1
                continue
            if textarea_type == 'check_code':
                check_code_size = check_code_size + 1
                continue
        if horizontal_size >= 2 and vertical_size >= 2:
            self.container_direction_type = 'mix'
            return
        if horizontal_size >= 2:
            self.container_direction_type = 'horizontal'
            return
        if vertical_size >= 2:
            self.container_direction_type = 'vertical'
            return
        if horizontal_size > 0 and vertical_size == 0:
            self.container_direction_type = 'horizontal'
            return
        if horizontal_size == 0 and vertical_size > 0:
            self.container_direction_type = 'vertical'
            return
